home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 301-325 / 325 / rexxhostlib / fancydemo.c < prev    next >
C/C++ Source or Header  |  1995-03-14  |  7KB  |  282 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by ???
  4.  *
  5.  *    Name .....: FancyDemo.c
  6.  *    Created ..: Monday 07-Mar-88 18:55
  7.  *    Revision .: 3
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    07-Jan-90       Olsen           Integrated rexxhost.library functions
  12.  *    16-Mar-88       Bill Hawes      Added result string return
  13.  *    07-Mar-88       Gary Samad      Created this file!
  14.  *
  15.  ****************************************************************************
  16.  *
  17.  * FancyDemo.c - A fancy rexx host that can send and receive messages.
  18.  *
  19.  * This is truly Public Domain!!
  20.  *
  21.  * $Revision Header ********************************************************/
  22.  #define REVISION 3
  23.  
  24. #include <libraries/dosextens.h>
  25. #include <libraries/dos.h>
  26.  
  27. #include <rexx/storage.h>
  28. #include <rexx/rxslib.h>
  29.  
  30. #include "rexxhostbase.h"
  31.  
  32. #define YES    1
  33. #define NO    0
  34.  
  35. #define OK    0
  36. #define NOTOK    1
  37.  
  38. #define EOS    '\0'
  39.  
  40. #define NO_REXX_MSG    "Rexx is not active.  Please run 'rexxmast' from another CLI.\n"
  41. #define STARTUP_MSG    "Type commands to rexx.  Type EOF (^\\) to end.\n"
  42. #define CLOSING_MSG    "Ok, we're closing (after all rexx messages have returned).\n"
  43.  
  44. #define WINDOW_SPEC    "CON:0/10/600/60/Fancy Demo Input Window/c"
  45. #define HOST_PORT_NAME    "FancyDemo"
  46. #define REXX_EXTENSION    "rexx"
  47.  
  48. #define BUFFLEN    100
  49.  
  50. extern struct Library    *OpenLibrary();
  51. extern struct MsgPort    *CreatePort();
  52. extern struct Message    *GetMsg();
  53.  
  54.     /* Since we don't need the RexxSysBase any more, we take
  55.      * the RexxHostBase (interface library).
  56.      */
  57.  
  58. struct RexxHostBase    *RexxHostBase;
  59.  
  60. struct MsgPort        *dos_reply_port = NULL;
  61. struct StandardPacket    *dos_message = NULL;
  62. struct MsgPort        *rexx_port = NULL;
  63. BPTR             window_file_handle = NULL;
  64. long             outstanding_rexx_commands = 0;
  65.  
  66.     /******** These are dos functions for getting and displaying user input *******/
  67.  
  68. struct StandardPacket *
  69. setup_dos_message()
  70. {
  71.     struct StandardPacket *malloc();
  72.     struct StandardPacket *new_packet;
  73.  
  74.         /* get a packet */
  75.  
  76.     if(new_packet = malloc(sizeof(struct StandardPacket)))
  77.     {
  78.         /* required AmigaDOS Kludge */
  79.  
  80.         new_packet -> sp_Msg . mn_Node . ln_Name = (char *)&(new_packet -> sp_Pkt);
  81.         new_packet -> sp_Pkt . dp_Link = &(new_packet -> sp_Msg);
  82.     }
  83.  
  84.     return(new_packet);
  85. }
  86.  
  87. void
  88. send_read_packet(dos_message,window_file_handle,dos_reply_port,buff)
  89. struct StandardPacket *dos_message;
  90. BPTR window_file_handle;
  91. struct MsgPort *dos_reply_port;
  92. char *buff;
  93. {
  94.     struct FileHandle *file_handle;
  95.  
  96.         /* change a BPTR to a REAL pointer */
  97.  
  98.     file_handle = (struct FileHandle *)(window_file_handle << 2);
  99.  
  100.         /* setup the packet for reading */
  101.  
  102.     dos_message -> sp_Pkt . dp_Arg1        = file_handle -> fh_Arg1;
  103.     dos_message -> sp_Pkt . dp_Arg2        = (long)buff;
  104.     dos_message -> sp_Pkt . dp_Arg3        = BUFFLEN;
  105.     dos_message -> sp_Pkt . dp_Type        = ACTION_READ;
  106.     dos_message -> sp_Pkt . dp_Port        = dos_reply_port;
  107.     dos_message -> sp_Msg . mn_ReplyPort    = dos_reply_port;
  108.  
  109.         /* now send it */
  110.  
  111.     PutMsg(file_handle -> fh_Type,dos_message);
  112. }
  113.  
  114. void
  115. close_up_shop(value)
  116. long value;
  117. {
  118.     if(window_file_handle)
  119.         Close(window_file_handle);
  120.  
  121.     if(dos_reply_port)
  122.         DeletePort(dos_reply_port);
  123.  
  124.     if(rexx_port)
  125.         DeleteRexxHost(rexx_port);
  126.  
  127.     if(dos_message)
  128.         free(dos_message);
  129.  
  130.     if(RexxHostBase)
  131.         CloseLibrary(RexxHostBase);
  132.  
  133.     exit(value);
  134. }
  135.  
  136. void
  137. main()
  138. {
  139.     long packet_out = NO;        /* whether a READ is outstanding */
  140.     char buff[BUFFLEN+1];        /* used for reading user input */
  141.     struct RexxMsg *rexxmessage;    /* incoming rexx messages */
  142.     long close_down = NO;        /* set when the user hits EOF */
  143.     STRPTR Arg;            /* Temporary string pointer */
  144.     UBYTE ArgBuff[40];        /* Temporary argument buffer */
  145.     LONG ArgCount;            /* Argument counter. */
  146.  
  147.         /* Try to open the rexxhost.library. */
  148.  
  149.     if(!(RexxHostBase = (struct RexxHostBase *)OpenLibrary("rexxhost.library",0)))
  150.     {
  151.         printf("couldn't open rexxhost library.\n");
  152.         close_up_shop(10);
  153.     }
  154.  
  155.         /* open a window to talk to the user through */
  156.  
  157.     if(!(window_file_handle = Open(WINDOW_SPEC,MODE_OLDFILE)))
  158.     {
  159.         printf("sorry, couldn't open a CON: window\n");
  160.         close_up_shop(10);
  161.     }
  162.  
  163.         /* set up a port for dos replys */
  164.  
  165.     if(!(dos_reply_port = CreatePort(NULL,0)))
  166.     {
  167.         printf("sorry, couldn't set up a dos_reply_port\n");
  168.         close_up_shop(10);
  169.     }
  170.  
  171.         /* set up a public port for rexx to talk to us later */
  172.  
  173.     if(!(rexx_port = CreateRexxHost(HOST_PORT_NAME)))
  174.     {
  175.         printf("sorry, couldn't set up our public rexx port\n");
  176.         close_up_shop(10);
  177.     }
  178.  
  179.         /* set up a dos packet for the asynchronous read from the window */
  180.  
  181.     if(!(dos_message = setup_dos_message()))
  182.     {
  183.         printf("sorry, not enough memory for a dos packet\n");
  184.         close_up_shop(10);
  185.     }
  186.  
  187.     Write(window_file_handle,STARTUP_MSG,sizeof(STARTUP_MSG));
  188.  
  189.         /* loop until quit and no messages outstanding */
  190.  
  191.     while(!close_down || outstanding_rexx_commands)
  192.     {
  193.             /* if the packet (for user input) has not been sent out, send it */
  194.  
  195.         if(!packet_out && !close_down)
  196.         {
  197.             /* send a packet to dos asking for user keyboard input */
  198.  
  199.             send_read_packet(dos_message,window_file_handle,dos_reply_port,buff);
  200.             packet_out = YES;
  201.         }
  202.        
  203.             /* now wait for something to come from the user or from rexx */
  204.  
  205.         Wait((1 << dos_reply_port -> mp_SigBit) | (1 << rexx_port -> mp_SigBit));
  206.  
  207.             /* got something!! */
  208.  
  209.             /* is it a command from the user? */
  210.  
  211.         if(GetMsg(dos_reply_port))
  212.         {
  213.                 /* not out any more */
  214.  
  215.             packet_out = NO;
  216.  
  217.                 /* if EOF (either the close gadget was hit or ^\) */
  218.  
  219.             if(dos_message -> sp_Pkt . dp_Res1 == 0)
  220.             {
  221.                 close_down = YES;
  222.                 Write(window_file_handle,CLOSING_MSG,sizeof(CLOSING_MSG));
  223.             }
  224.             else
  225.             {
  226.                     /* NULL terminate the string (thanks again DOS!) */
  227.  
  228.                 buff[dos_message -> sp_Pkt . dp_Res1 - 1] = EOS;
  229.  
  230.                     /* send the command directly to rexx */
  231.  
  232.                 if(!SendRexxCommand(rexx_port,buff,NULL,NULL))
  233.                     Write(window_file_handle,NO_REXX_MSG,sizeof(NO_REXX_MSG));
  234.                 else
  235.                     outstanding_rexx_commands++;
  236.             }
  237.         }
  238.  
  239.             /* did we get something from rexx? */
  240.  
  241.         while(rexxmessage = (struct RexxMsg *)GetMsg(rexx_port))
  242.         {
  243.                 /* Getting a string pointer means
  244.                  * that we've received a command.
  245.                  */
  246.  
  247.             if(Arg = GetRexxCommand(rexxmessage))
  248.             {
  249.                 LONG CharCount = 0; /* Need counter, function reentrant. */
  250.  
  251.                 printf("Got \"%s\" from Rexx.\n",Arg);
  252.  
  253.                     /* Now split the command string into arguments. */
  254.  
  255.                 ArgCount = 0;
  256.  
  257.                 while(GetToken(Arg,&CharCount,ArgBuff,40))
  258.                     printf("Argument %ld = \"%s\"\n",ArgCount++,ArgBuff);
  259.  
  260.                 if(!strcmp(Arg,"BAD"))
  261.                     ReplyRexxCommand(rexxmessage,10,0,"A Test");
  262.                 else
  263.                     ReplyRexxCommand(rexxmessage,0,0,"A Test");
  264.             }
  265.             else
  266.             {
  267.                     /* Now, spill the args... */
  268.  
  269.                 printf("The command \"%s\" has terminated with code %ld, %ld.\n",
  270.                     GetRexxArg(rexxmessage),GetRexxResult1(rexxmessage),GetRexxResult2(rexxmessage));
  271.  
  272.                 FreeRexxCommand(rexxmessage);
  273.                 outstanding_rexx_commands--;
  274.             }
  275.         }
  276.     }
  277.  
  278.         /* clean up */
  279.  
  280.     close_up_shop(0);
  281. }
  282.